$._ae_ = {
  ping: function () {
    return 'success'
  },
  render: function () {
    // 参考frame io
    return '待实现'
  },
  getActiveItem: function () {
    return app.project.activeItem
  },
  createProject: function (path) {
    return app.project.save(new File(path))
  },
  saveProject: function () {
    return app.project.file && app.project.save()
  },
  getProject: function () {
    return app.project.file && JSON.stringify(app.project.file)
  },
  getProjectPath: function () {
    return app.project.file && app.project.file.fsName;
  },
  getProjectName: function () {
    return app.project.file && app.project.file.displayName;
  },
  getAllItems: function (folderGroup) {
    folderGroup = folderGroup || app.project.rootFolder
    var result = [];
    var numItems = folderGroup.numItems;
    for (var i = 1; i <= numItems; i++) {
      var item = folderGroup.item(i);
      // 判断当前属性是否为文件夹，如果是，则继续遍历
      if (item instanceof FolderItem) {
        result.push(JSON.parse(getAllItems(item)))
        continue
      }
      result.push(item);
    }
    return JSON.stringify(result);
  },
  getAllFootageItems: function () {
    var result = [];
    var items = this.getAllItems()
    for (var i = 0; i < items.length; i++) {
      var item = items[0];
      if (item && item.mainSource instanceof FileSource) {
        var file = item.file.fsName;
        if (item.file.exists && file != '') {
          result.push(file);
        }
      }
    }
    if (result.length > 0) {
      return JSON.stringify(result);
    } else {
      return '[]';
    }
  },
  getAllCompItems: function () {
    var result = [];
    var items = this.getAllItems()
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      if (item && item instanceof CompItem) {
        result.push(item.name);
      }
    }
    if (result.length > 0) {
      return JSON.stringify(result);
    } else {
      return '[]';
    }
  },
  getTextLayersByCompNameOrId: function (value, key) {
    key = key || 'name'
    var result = [];
    var items = this.getCompItems()
    var item = items.filter(function (item) {
      return item[key] === value
    })[0]
    if (!item) return '[]'
    for (var j = 1; j <= item.numLayers; j++) {
      var layer = item.layers[j];
      if (layer instanceof TextLayer && layer.active) {
        result.push({
          CompItem: item.name,
          Key: layer.name,
          Value: layer.property('Source Text').value.text,
          LayerIndex: layer.index
        });
      } else if (layer instanceof AVLayer && layer.source) {
        var arr = this.getTextLayersByCompNameOrId(layer.source.id, 'id');
        for (var k = 0; k < arr.length; k++) {
          result.push(arr[k]);
        }
      }
    }
    if (result.length > 0) {
      return JSON.stringify(result);
    } else {
      return '[]';
    }
  },
  getFilesOfCompItems: function () {
    var result = [];
    var compItems = this.getCompItems()
    for (var i = 0; i < compItems.length; i++) {
      var item = compItems[i];
      for (var j = 1; j <= item.numLayers; j++) {
        var layer = item.layers[j];
        if (layer instanceof AVLayer) {
          if (layer.source instanceof FootageItem && layer.source.file) {
            result.push(layer.source.file);
          }
        }
      }
    }
    if (result.length > 0) {
      return JSON.stringify(result);
    } else {
      return '[]';
    }
  },
  importFile: function (path) {
    // 后续处理跟pr同样处理
    return app.project.importFile(new ImportOptions(File(decodeURIComponent(path))));
  },
  replaceFile: function (item, newPath) {
    var file = new File(decodeURIComponent(newPath));
    if (file && file.exists) {
      file.close();
      return JSON.stringify({
        code: 0,
        data: item.replace(newPath)
      })
    } else {
      return JSON.stringify({
        code: 1,
        msg: 'not exist'
      })
    }
  },
}